﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Collections;
using System.Reflection;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;
using System.ComponentModel;
using System.Runtime.Serialization.Formatters.Binary;

namespace Dracham.Classes
{
    public class ServerManagement
    {
        public static void Login(DrachamNetwork client)
        {
            String username = client.ReceiveString(50);
            String password = client.ReceiveString(50);

            if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
            {
                if (username == GameWorld.Settings["ManagementUsername"] && password == GameWorld.Settings["ManagementPassword"])
                {
                    string sessionKey = Guid.NewGuid().ToString();
                    GameWorld.ConsoleClients.Add(new KeyValuePair<DrachamNetwork, String>(client, sessionKey));
                    client.Invoke(MessageType.CLoginSuccess, new NetworkParameter(sessionKey, 36));
                    client.AddHandler(MessageType.CXmlDataRequest, new HandlerMethod(ServerManagement.DataRequest));
                    return;
                }
            }

            client.Invoke(MessageType.CLoginFailure, null);
        }

        public static void DataRequest(DrachamNetwork client)
        {
            string sessionKey = client.ReceiveString(36);
            if (GameWorld.ConsoleClients.Single(c => c.Key == client).Value == sessionKey)
            {
                string className = client.ReceiveString(30);

                switch (className)
                {
                    case "Character":
                        List<Character> c = new List<Character>();
                        GameWorld.Accounts.ForEach(delegate(Account a)
                        {
                            c.AddRange(a.Characters);
                        });
                        ServerManagement.SendClassData(client, c);
                        break;
                    default:
                        break;
                }
            }
        }

        private static void SendClassData(DrachamNetwork client, IList list)
        {
            if (list.Count == 0) return;
            
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
            xmlWriter.Formatting = Formatting.Indented;
            try
            {
                new XmlSerializer(list.GetType()).Serialize(xmlWriter, list);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + Environment.NewLine + e.InnerException.Message);
                return;
            }

            xmlWriter.Flush();

            String xmlData = stringWriter.ToString();

            Console.WriteLine(xmlData);

            client.Invoke(MessageType.CXmlDataLength, new NetworkParameter(xmlData.Length.ToString(), 6));
            client.Invoke(MessageType.CXmlData, new NetworkParameter(xmlData, xmlData.Length));
        }
    }
}
